ifndef CROSS_COMPILE
CROSS_COMPILE =  riscv32-unknown-elf
endif
CC = $(CROSS_COMPILE)-gcc
LD = $(CROSS_COMPILE)-ld
OBJCOPY = $(CROSS_COMPILE)-objcopy
OBJDUMP = $(CROSS_COMPILE)-objdump
PYTHON	= python

OBJECTS = inst_rom.o main.o trm.o

ARCH=rv32i
MABI=ilp32

CFLAG0=-static -nostdlib -nostartfiles -lm -lgcc -march=$(ARCH) -mabi=$(MABI)
CFLAG1=-DPREALLOCATE=1 -mcmodel=medany -static -std=gnu99 -O0 -ffast-math -fno-common -fno-builtin-printf
CFLAG_TEST=-DPREALLOCATE=1 -mcmodel=medlow -static -std=gnu99 -O0 -fno-common -fno-builtin-print

export	CROSS_COMPILE

# ********************
# Rules of Compilation
# ********************

all: inst_rom.om inst_rom.bin inst_rom.dump inst_rom.data

main.o: main.c
	$(CC) -c $< $(CFLAG0) $(CFLAG1) $(CFLAG_TEST)

inst_rom.o: inst_rom.s
	$(CC) -c $< $(CFLAG0) $(CFLAG1) $(CFLAG_TEST)

trm.o: trm.c
	$(CC) -c $< $(CFLAG0) $(CFLAG1) $(CFLAG_TEST)

inst_rom.om: ram.ld $(OBJECTS)
	$(LD) -T ram.ld $(OBJECTS) -o $@

inst_rom.bin: inst_rom.om
	$(OBJCOPY) -O binary $<  $@
inst_rom.dump: inst_rom.om
	$(OBJDUMP) -D $< > $@
inst_rom.data: inst_rom.bin
	./Bin2Mem -f $< -o $@
inst_rom.coe: inst_rom.data
	$(PYTHON) data2coe.py
inst_rom.mif: inst_rom.data
	$(PYTHON) data2mif.py
clean:
	rm -f *.o *.om *.bin *.data *.mif *.asm
